function network(api,path,action=""){
    console.log(api)
    console.log(path)
    var apis = {"":"","cv-api":"http://127.0.0.1:8001"}
    const request = new XMLHttpRequest();
    request.withCredentials = true;
    request.timeout=180000;
    request.open("GET",apis[api]+path);
    request.send();
    request.onload = () => {
        try{
            data = request.response
            data = eval("("+data+")")
            if(action==""){
                return data
            }else{
                if(request.status==200){
                    action(data)
                }else{
                    showError(request.status)

                }
            }
            try{
                if(data.success==false){
                    showError(data.msg)
                }

            }catch(e){}
        }catch(e){
            // showError("服务器错误")
            action({"success":false,"msg":"服务器错误","code":request.status})
        }
    }
    request.onerror = (e) => {
        showErrorr("网络错误")
        action({"success":false,"msg":"网络错误","code":request.status})
    }
}
function post(api,path,data,action=""){
    console.log(api)
    console.log(path)
    var apis = {"":"","cv-api":"http://127.0.0.1:8001"}
    const request = new XMLHttpRequest();
    request.withCredentials = true;
    request.open("POST",apis[api]+path);
    request.setRequestHeader('Content-Type', 'application/json');
    request.send(JSON.stringify(data));
    request.onload = () => {
        try{
            data = request.response
            data = eval("("+data+")")
            if(action==""){
                return data
            }else{
                console.log(request.status)
                if(request.status==200){
                    action(data)
                }else{
                    showError(request.status)

                }
            }
            try{
                if(data.success==false){
                    showError(data.msg)
                }

            }catch(e){}
        }catch(e){
            // showError("服务器错误")
            action({"success":false,"msg":"网络错误","code":request.status})
        }
    }
    request.onerror = (e) => {
        showError("网络错误")
        action({"success":false,"msg":"网络错误","code":request.status})
    }
}
function getUrlParams() {
    try{
        let url = window.location.href
        let urlStr = url.split('?')[1]
        let obj = {};
        let paramsArr = urlStr.split('&')
        for(let i = 0,len = paramsArr.length;i < len;i++){
            let arr = paramsArr[i].split('=')
            obj[arr[0]] = arr[1];
        }
        return obj
    }catch{return null}
}
function preventDefault(e) {
    e.preventDefault();
  }
function disableScroll() {
    // 禁用触摸滚动
    document.addEventListener('touchmove', preventDefault, { passive: false });
    // 禁用鼠标滚动
    document.addEventListener('mousewheel', preventDefault, { passive: false });
    // 禁用滚轮事件（大多数浏览器）
    document.addEventListener('wheel', preventDefault, { passive: false });
    // 禁用Firefox的滚动事件
    document.addEventListener('DOMMouseScroll', preventDefault, { passive: false });
}
function enableScroll() {
    // 移除触摸滚动事件监听器
    document.removeEventListener('touchmove', preventDefault, { passive: false });
    // 移除鼠标滚动事件监听器
    document.removeEventListener('mousewheel', preventDefault, { passive: false });
    // 移除滚轮事件监听器
    document.removeEventListener('wheel', preventDefault, { passive: false });
    // 移除Firefox的滚动事件监听器
    document.removeEventListener('DOMMouseScroll', preventDefault, { passive: false });
}
function file_select(multiple,accept){
    return new Promise(function (resolve, reject) {
        var input = document.createElement('input');
        input.type = 'file';
        input.multiple = multiple;
        input.accept=accept;
        input.style.display = 'none';
        input.addEventListener('change', function() {
            if(input.files.length==0){
                reject(false)
            }else{
                resolve(input.files)
            }
        });
        document.body.appendChild(input);
        input.click();
    });
}
async function upload_img(file,service){
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest(); // 创建一个新的XMLHttpRequest对象
        // var fileInput = document.getElementById("file_input");
        xhr.open('POST', 'http://127.0.0.1:8001/files/upload/'+service, true); // 设置请求类型和目标URL
        xhr.withCredentials=true
        xhr.onload = function() {
            if (this.status === 200) { // 如果请求成功，说明文件已成功上传
                console.log('文件上传成功!'); // 在控制台打印成功信息
                udata = xhr.response
                udata = eval("("+udata+")")
                console.log(udata)
                resolve(udata)
            } else { // 如果请求失败，可能需要处理错误情况
                reject({"success":false,"msg":"网络错误","code":this.status})
            }
        };
        var formData = new FormData(); // 创建一个新的FormData对象，用于存储文件数据和其他数据
        formData.append('file', file); // 将文件添加到FormData对象中
        xhr.send(formData); // 上传文件数据和其他数据到服务器
    });
}
function out_error(msg){
    var e = document.createElement("div")
    e.innerHTML=`
    <div style="top: 80px;left: 50%;transform: translate(-50%);position: fixed;width: auto;height:auto;padding: 20px;border-radius: 25px;display: flex;background-color: red;">
        <p style="margin: auto;color: white;font-size: 20px;">${msg}</p>
    </div>
    `
    document.body.appendChild(e)
    setTimeout(function(){
        e.remove()
    },3000)
}
console.log("request tool load success")

function showError(code) {
    // 创建外层提示框
    const errorBox = document.createElement('div');
    errorBox.style.position = 'fixed';
    errorBox.style.top = '-100px';
    errorBox.style.left = '50%';
    errorBox.style.transform = 'translateX(-50%)';
    errorBox.style.padding = '20px 30px';
    errorBox.style.backgroundColor = '#ff4d4d';
    errorBox.style.color = 'white';
    errorBox.style.fontSize = '16px';
    errorBox.style.borderRadius = '12px';
    errorBox.style.boxShadow = '0 4px 12px rgba(0,0,0,0.2)';
    errorBox.style.zIndex = '9999';
    errorBox.style.opacity = '0';
    errorBox.style.transition = 'opacity 0.5s ease, top 0.5s ease';
    errorBox.style.textAlign = 'center';

    // 创建文字内容
    const text = document.createElement('div');
    text.textContent = code;
    errorBox.appendChild(text);

    // 添加到 body
    document.body.appendChild(errorBox);

    // 触发动画显示
    setTimeout(() => {
        errorBox.style.top = '20px';
        errorBox.style.opacity = '1';
    }, 100);

    // 5秒后自动消失（淡出）
    setTimeout(() => {
        errorBox.style.opacity = '0';
        errorBox.style.top = '-100px';

        // 完全隐藏后移除元素
        setTimeout(() => {
            if (errorBox.parentNode) {
                errorBox.remove();
            }
        }, 500);
    }, 5000);
}